CFLAGS := -O2 -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wmissing-declarations -fno-strict-aliasing
CFLAGS += -Werror -Wformat -Wformat-security -D_GNU_SOURCE -fPIC -fstack-protector-all -fno-common
CFLAGS += `pkg-config --cflags --libs glib-2.0`

LDFLAGS := -fPIE -pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack,-s
LDFLAGS += `pkg-config --cflags --libs glib-2.0`

VPATH = src:include

CC := gcc
LIBS := -lgnutls -lnettle -lcjson
INC := -I./include
OBJS := qt-enclave
objects = util.o image.o enclave.o cli.o logger.o

%.o: %.c
	$(CC) $(CFLAGS) $(INC) -o $@ -c $<

$(OBJS): $(objects)
	$(CC) $(LDFLAGS) -o $@ $(objects) $(LIBS)

CLEANFILES := $(objects) $(OBJS)

.PHONY: all clean

all: $(OBJS)

install:
	mkdir -p -m 755 /usr/local/bin/qingtian
	mkdir -p -m 755 /usr/local/bin/qingtian/enclave
	mkdir -p -m 755 /etc/qingtian/enclave
	mkdir -p -m 755 /usr/local/share/qingtian
	mkdir -p -m 755 /usr/local/share/qingtian/enclave
	mkdir -p -m 755 /usr/lib/systemd/system/
	mkdir -p -m 755 /var/log/qingtian_enclaves/
	install -p -m 750 scripts/qt_enclave_env /usr/local/bin/qingtian/enclave/qt-enclave-env
	install -p -m 750 scripts/qt /usr/local/bin/qt
	install -p -m 750 conf/qt_enclave_env.conf /etc/qingtian/enclave/qt-enclave-env.conf
	install -p -m 750 conf/qt_enclave_env.service /usr/lib/systemd/system/qt-enclave-env.service
	install -p -m 750 platform/linuxkit/linuxkit-linux-amd64 /usr/local/share/qingtian/enclave/
	install -p -m 750 $(OBJS) /usr/local/bin/qingtian

clean:
	rm -f ${CLEANFILES}
